import 'dart:convert' show json;

import 'package:freedom/entity/card/mahjong_card_entity.dart';
import 'package:freedom/util/data_type.dart';

class UserTouchBar {
  UserTouchBar({
    this.userId,
    this.cardEnum,
    this.cards,
  });

  factory UserTouchBar.fromJson(Map<String, dynamic>? jsonRes) {
    if (jsonRes == null) {
      return UserTouchBar();
    }
    final List<MahjongCardEntity>? cards =
        jsonRes['cards'] is List ? <MahjongCardEntity>[] : null;
    if (cards != null) {
      for (final dynamic item in jsonRes['cards']) {
        if (item != null) {
          cards.add(MahjongCardEntity(cardId: item));
        }
      }
    }

    return UserTouchBar(
      userId: asT<String>(jsonRes['userId']),
      cardEnum: asT<String>(jsonRes['cardEnum']),
      cards: cards,
    );
  }

  String? userId;
  String? cardEnum;
  List<MahjongCardEntity>? cards;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'userId': userId,
        'cardEnum': cardEnum,
        'cards': cards,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
